<?php
namespace app\home\model;
use think\Model;

class OrderAwardModel extends Model
{
	protected $table  = 'qm_order_award';



	/**
	*
	*订单-是否存在奖励
	*
	**/
	public function checkOrderIdEarnings($order_id, $field = "*")
	{
		$list = $this->where('order_id', '=', $order_id)->field($field )->find();
		return $list;
	}


	/**
	*
	*
	*用户-个人收益总金额
	**/
	public function memberOrderEarnings($member_id)
	{
		$money = $this->where('member_id', '=', $member_id)->where('award_type', 'in', [1,2])->where('status', '=', 2)->sum('money');
		return $money;
	}



	/**
	*
	*
	*用户-累计收益
	**/
	public function memberOrderCountsEarnings($member_id)
	{
		$money = $this->where('member_id', '=', $member_id)->where('status', '=', 2)->sum('money');
		return $money;
	}



	/**
	*
	*
	*预估收益 - 本月，全部
	**/
	public function getMemberOrderPredictEarnings($member_id, $if_all = 0)
	{
		if($if_all != 1)
		{
			//本月
			$atart = date('Y-m-01',time());
			$end = date('Y-m-t 23:59:59',time());
			$where = [
				['add_time','>=', $atart],
				['add_time', '<=', $end]
			];
			$money = $this->where('member_id', '=', $member_id)->where('status', '=', 1)->where($where)->sum('money');
		}else{
			$money = $this->where('member_id', '=', $member_id)->where('status', '=', 1)->sum('money');
		}

		return $money;
	}



	/**
	*
	*添加收益
	*
	**/
	public function addMemberOrderEarnings($data)
	{
		$res = $this->insertGetId($data);
		return $res;
	}



	/**
	*
	*订单-已计算的奖励列表
	*
	**/
	public function getOrderIdEarningsList($order_id, $field = "*")
	{
		$list = $this->where('order_id', '=', $order_id)->where('status', '=', 1)->field($field )->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}


	/**
	*
	*发放奖励状态
	*
	**/
	public function updateOrderIdEarnings($id, $update)
	{
		$res = $this->where('id', '=', $id)->where('status', '=', 1)->update($update);
	}


	/**
	*
	*
	*用户奖励-列表
	**/
	public function getMemberAwardList($where, $page = 1, $field = '*')
	{
		$numbers = 10; 
		$keys = ($page - 1) * $numbers;

		$list = $this->where($where)->field($field)->limit($keys, $numbers)->order('add_time DESC')->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		return $list;
	}



	//搜索条件,获得用户数据总额
	public function getMermberCountsWhere($where, $sum = '*')
	{
		if($sum == '*')
		{
			$counts = $this->where($where)->group('order_id')->count($sum);
		}else{
			$counts = $this->where($where)->sum($sum);
		}
		
		return $counts;
	}
}